package leetcode_100;


/**
 *@author 周杨
 *RemoveDuplicatesFromSortedList_83 从一个有序链表中删除重复的节点 只保存唯一值
 *describe: AC 43%
 *2018年5月15日 下午2:04:52
 */
public class RemoveDuplicatesFromSortedList_83 {
	static class ListNode{
		int val;
		ListNode next;
		ListNode(int x){val = x;};
	}
	public static void main(String[] args) {
		RemoveDuplicatesFromSortedList_83 test=new RemoveDuplicatesFromSortedList_83();
		ListNode a=new ListNode(1);
		ListNode b=new ListNode(1);
		ListNode c=new ListNode(1);
		ListNode d=new ListNode(2);
		ListNode e=new ListNode(3);
		ListNode f=new ListNode(5);
		ListNode g=new ListNode(5);
		
		a.next=b;
		b.next=c;
		c.next=d;
		d.next=e;
		e.next=f;
		f.next=g;
		ListNode res=test.deleteDuplicates(a);

	}
	
	public ListNode deleteDuplicates(ListNode head) {
		if(head==null||head.next==null)
			return head;
		//因为head必须被保留 所以不用判断head是否是废弃节点 直接从下一个节点判断
		ListNode nowNode=head,run=head.next;
		int nowVal=head.val;
		while(run!=null) {
			if(run.val!=nowVal) {//可以加入链表
				nowNode.next=run;
				nowNode=run;
				nowVal=run.val;
			}
			run=run.next;
		}
		nowNode.next=null;
		return head;
	}

}
